import { getView, getDevice } from '../../../api/getData.js';
const app = getApp()

Component({
  properties: {
    treeData: {
      type: Object
    }
  },

  data: {
    open: false,
    isBranch: false,
    treeList: [],
    path: [],
    userName: '',
    objArr: [],
  },
  ready: function (e) {
    this.setData({
      isBranch: Boolean(this.data.treeData.children && this.data.treeData.children.length),
    });
    this.setData({
      userName: wx.getStorageSync('username')
    })
  },
  methods: {
    showNextList (e) {
      let { item: { treeID, sceneID, treeName, parent }, index } = e.currentTarget.dataset;
      if (parent === 0) {
        this.toggle();
        return
      }
      this.handler(app.globalData.result);
      this.find(treeID);
      app.globalData.deatilNav = Array.from(new Set(this.data.path.reverse()));
      if (app.globalData.deatilNav.length > 1) {
        app.globalData.deatilNav = app.globalData.deatilNav.splice(1, app.globalData.deatilNav.length)
        app.globalData.deatilNav.push('')
      }
      wx.setStorageSync('deatilNav', app.globalData.deatilNav);
      wx.setStorageSync('treeInfo', e.currentTarget.dataset.item);
      app.globalData.treeInfo = e.currentTarget.dataset.item;
      if (e.currentTarget.dataset.item.children && e.currentTarget.dataset.item.children.length) { // 有子节点
        this.toggle();
        app.globalData.isState = false;
        this.getViewData({
          treeID,
          sceneID,
          devices: false
        })
      } else { // 没有子节点
        app.globalData.showNav = false;
        app.globalData.isState = true;
        app.globalData.deatilList = [];
        this.getViewData({
          treeID,
          devices: true
        })
      }
    },
    toggle (e) {
      this.setData({
        open: !this.data.open,
      })
    },
    // 获取总览信息
    getViewData(params) {
      try {
        let { devices, treeID, parent, sceneID } = params;
        if (devices) { // 最后一个节点的子数据
          getDevice(treeID).then(res => {
            this.formatGetView(res)
            app.globalData.deviceList = res.result;
          })
        } else {
          getView(treeID, sceneID, devices).then(res => {
            console.log('getView')
            this.formatGetView(res)
          })
        }
      } catch(e) {
        throw e;
      }
    },

    // 处理总览数据 
    formatGetView(res) {
      if (res.resCode == 'SUCCESS') {
        let [userName, isState] = [this.data.userName, app.globalData.isState];
        if (isState && userName == 'tobaccoxiamen') { // 厦门卷烟厂
          this.countStatus(res.view || res.result);
        }
        app.globalData.deatilList = res.result.view || res.result
        wx.setStorageSync('deatilList', res.result.view || res.result)
      }
    },
    // 根据treeID查找treeName
    handler(tree) {
      tree.map(item => {
        this.data.treeList.push(item);
        if (item.children) {
          this.handler(item.children)
        }
      })
      this.setData({
        treeList: this.data.treeList
      })
    },
    find(treeID) {
      let active = this.data.treeList.find(item => {
        return item.treeID === treeID
      });
      if (active !== undefined) {
        this.data.objArr.push(active);
        this.data.path.push(active.treeName);
        this.setData({
          path: this.data.path
        })
        if (active.parent) {
          this.find(active.parent)
        }
      }
    },
    // 查找出现最多次的状态 针对厦门卷烟厂
    countStatus (val) {
      if (val.length === 0) {
        return;
      }
      if (val.length === 1) {
        app.globalData.mostStatus = val[0].status;
        return;
      }
      let obj = {};
      val.forEach(item => {
        if (obj[item.status]) {
          obj[item.status]++;
        } else {
          obj[item.status] = 1;
        }
      });
      let most = '',
        mostNum = 0;
      for (let key of Object.keys(obj)) {
        if (obj[key] > mostNum) {
          mostNum = obj[key];
          most = key;
        }
      }
      app.globalData.mostStatus = most;
    }
  }
})